<?php

class Account extends Model {

	var $account_id = 0;
	var $department_id = 0;

	var $user = '';
	var $password = '';
	var $active = true;

	var $first_name = '';
	var $last_name = '';
	var $email = '';
	var $last_login = '';

	var $is_admin = false;
	var $is_active = false;
	var $is_manager = false;

	function Account() {
		parent::Model();

		$account_id = $this->session->userdata('account_id');
		$this->account_id = $account_id;
		if( $account_id ) {
			
			$sql = 'select * from `accounts` where `account_id`=?';
			$r = $this->db->query( $sql, array($account_id) );
			if( $r->num_rows() == 0 ) {
				return;
			}
			$acc = $r->row();

			$this->department_id = $acc->department_id;

			$this->user = $acc->user;
			$this->password = $acc->password;

			$this->first_name = $acc->first_name;
			$this->last_name = $acc->last_name;
			$this->email = $acc->email;
			$this->last_login = $acc->last_login;

			$this->is_admin = (bool)$acc->is_admin;
			$this->is_active = (bool)$acc->is_activ;
			$this->is_manager = (bool)$acc->is_manager;
		}
	}

	function is_guest() {
		return $this->account_id == null;
	}
	
	function is_admin() {
		return $this->is_admin;
	}

	function is_manager() {
		return $this->is_manager;
	}

	function login( $user, $password ) {
		$sql = 'select * from `accounts` where `user`=? and `password`=?';
		$r = $this->db->query( $sql, array($user, md5($password)) );
		if( $r->num_rows() == 0 ) {
			return false;
		}
		$d = $r->row();
		$this->session->set_userdata( 'account_id', $d->account_id );
		return true;
	}

	function logout() {
		$this->session->sess_destroy();
	}
}

?>
